<?php

declare(strict_types=1);

namespace app\controller;

use app\param\UserParam;
use app\request\UserRequest;
use app\service\UserService;
use Dsxwk\Framework\Exception\CodeException;
use support\Response;
use support\Request;

class UserController
{
    /**
     * 列表
     *
     * @param Request     $request
     * @param UserService $service
     *
     * @return Response
     */
    public function index(Request $request, UserService $service): Response
    {
        return apiResponse($service->index());
    }

    /**
     * 创建
     *
     * @param Request     $request
     * @param UserService $service
     *
     * @return Response
     * @throws CodeException
     */
    public function create(Request $request, UserService $service): Response
    {
        $result = validated(UserRequest::class);

        $param = new UserParam($result);

        return apiResponse($service->create($param));
    }

    /**
     * 异常测试
     *
     * @param UserService $service
     *
     * @return Response
     * @throws CodeException
     */
    public function exception(UserService $service): Response
    {
        return apiResponse($service->exception());
    }
}